<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="key" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="columnKey">
              <a-input v-model="model.columnKey" placeholder="请输入key"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="columnName">
              <a-input v-model="model.columnName" placeholder="请输入名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="columnType">
                 <j-dict-select-tag
                    type="list"
                    v-model="model.columnType"
                    dictCode="zb_file_varibale_type"
                    placeholder="请选择类型"
                  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否开标显示" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <j-switch v-model="model.ifShow"  ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="columnSize">
              <a-input v-model="model.columnSize" placeholder="请输入长度"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ExTenderFileSetVariableForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
     validatorRules: {
        columnKey: [
          { required: true, message: '请输入key', trigger: 'blur' }
        ],
        columnName: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        columnType: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ]
      },
      url: {
        add: '/exTenderFileSetVariable/exTenderFileSetVariable/add',
        edit: '/exTenderFileSetVariable/exTenderFileSetVariable/edit',
        queryById: '/exTenderFileSetVariable/exTenderFileSetVariable/queryById',
      },
      projectInfo: { projectId: '', sectionId: '' },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(_projectInfo) {
      this.projectInfo.projectId = _projectInfo.projectId
      this.projectInfo.sectionId = _projectInfo.sectionId
      this.edit({ ...this.modelDefault,...this.projectInfo })
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.projectInfo.projectId = record.projectId
      this.projectInfo.sectionId = record.sectionId
      console.log('Editing with record:', record); // 调试日志
      this.visible = true
    },
    submitForm() {
      const that = this
      // debugger
       console.log('Project Info:', this.projectInfo); // 调试日志
      that.model.projectId = this.projectInfo.projectId
      that.model.sectionId = this.projectInfo.sectionId
      console.log('Final model before submit:', this.model); // 调试日志
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
